<?php

namespace app\admin\controller;

use app\common\model\SystemUser;
use buildView\Button;
use think\Controller;
use buildView\Form;
use buildView\Grid;
use app\common\model\ShopUserSign;
/**
 * 打卡管理
 * Class Sign
 * @package app\admin\controller
 */
class Sign extends Controller
{
    /**
     * 列表
     * @auth true
     * @menu true
     * @return string
     */
    public function index()
    {
        $store = session('store');
        $grid = new Grid(new ShopUserSign);
        if ($store){
            if (key_exists('staff_id',$store)){
                $grid->model()->where('store_id',$store['id'])->where('staff_id',$store['staff_id']);
            }else{
                $grid->model()->where('store_id',$store['id']);
            }
        }else{
            $grid->store()->name('门店');
        }
        $grid->staff()->name('姓名');
		$grid->column('sign_time','打卡时间');
		$grid->filter(function ($filter){
            $filter->dateBetween('sign_time','打卡时间');
        });
        $button = new Button('打卡');
        $button->href(url('sign').'?back=1', 'modal');
        $grid->hideAddButton();
        $grid->hideDeletesButton();
        $grid->hideAction();
        $grid->addTools($button);
        $grid->actions(function ($action){
        	 $action->hideDetail();
        });
        return $grid->view();
    }

    /**
     * 打卡
     * @auth true
     * @menu true
     * @return string
     */
    public function sign()
    {
        $form = new Form(new ShopUserSign);
        $time = date('Y-m-d H:i:s',time());
        $form->text('sign_time','打卡时间')->default($time)->readonly();
        $form->saving(function($post){
            $store = session('store');
            if (empty($store['staff_id'])){
                $this->error('暂无权限打卡');
            }
            $post['store_id'] = $store['id'];
            $post['staff_id'] = $store['staff_id'];
            return $post;
        });
        $form->setSubmitText('确认打卡');
        return $form->view();
    }


}
